home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 September / PCWorld_2002-09_cd.bin / Software / Vyzkuste / httrack / httrack-3.20RC4.exe / {app} / HelpHtml / div / search.sh
Linux/UNIX/POSIX Shell Script  |  2001-04-30  |  2KB  |  60 lines

  1. #!/bin/sh
  2.  
  3. # Simple indexing test using HTTrack
  4. # A "real" script/program would use advanced search, and 
  5. # use dichotomy to find the word in the index.txt file
  6. # This script is really basic and NOT optimized, and
  7. # should not be used for professional purpose :)
  8.  
  9. TESTSITE="http://localhost/"
  10.  
  11. # Create an index if necessary
  12. if ! test -f "index.txt"; then
  13.     echo "Building the index .."
  14.     rm -rf test
  15.     httrack --display "$TESTSITE"  -%I -O test
  16.     mv test/index.txt ./
  17. fi
  18.  
  19. # Convert crlf to lf
  20. if test "`head index.txt -n 1 | tr '\r' '#' | grep -c '#'`" = "1"; then
  21.     echo "Converting index to Unix LF style (not CR/LF) .."
  22.     mv -f index.txt index.txt.old
  23.     cat index.txt.old|tr -d '\r' > index.txt
  24. fi
  25.  
  26. keyword=-
  27. while test -n "$keyword"; do
  28.     printf "Enter a keyword: "
  29.     read keyword
  30.  
  31.     if test -n "$keyword"; then
  32.         FOUNDK="`grep -niE \"^$keyword\" index.txt`"
  33.  
  34.         if test -n "$FOUNDK"; then    
  35.             if ! test `echo "$FOUNDK"|wc -l` = "1"; then
  36.                 # Multiple matches
  37.                 printf "Found multiple keywords: "
  38.                 echo "$FOUNDK"|cut -f2 -d':'|tr '\n' ' '
  39.                 echo ""
  40.                 echo "Use keyword$ to find only one"
  41.             else
  42.                 # One match
  43.                 N=`echo "$FOUNDK"|cut -f1 -d':'`
  44.                 PM=`tail +$N index.txt|grep -nE "\("|head -n 1`
  45.                 if ! echo "$PM"|grep "ignored">/dev/null; then
  46.                     M=`echo $PM|cut -f1 -d':'`
  47.                     echo "Found in:"
  48.                     cat index.txt | tail "+$N" | head -n "$M" | grep -E "[0-9]* " | cut -f2 -d' '
  49.                 else
  50.                     echo "keyword ignored (too many hits)"
  51.                 fi
  52.                     fi
  53.         else
  54.             echo "not found"
  55.         fi
  56.  
  57.     fi
  58. done
  59.  
  60.